package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import com.jy.wms.service.WmsOrderRepairService;
import com.jy.wms.service.WmsShipmentOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsShipmentOrderController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Wed Oct 18 11:12:04 GMT+08:00 2017
 */
@RestController
@RequestMapping("/wmsshipmentorder")
@Api(value = "/wmsshipmentorder", description = "关于发运订单操作")
public class WmsShipmentOrderController extends BaseController<WmsShipmentOrderService, WmsShipmentOrder> {
    @Autowired
    private WmsOrderRepairService wmsOrderRepairService;


    @ApiOperation(value = "生成订单编号", response = JSONObject.class, notes = "生成订单编号")
    @GetMapping(value = "/getBh")
    public @ResponseBody
    JSONObject getBh() {
        JSONObject json = getJSONObject();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        String bh = df.format(new Date());
        json.put("bh", bh);
        return json;
    }

    @PostMapping(value = "list")
    @ApiOperation(value = "查询信息", response = JSONObject.class, notes = "查询信息")
    public @ResponseBody
    ResultPage<WmsShipmentOrder> getWmsShipmentOrder(@RequestBody WmsShipmentOrder entity, HttpSession session) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();

        }
        Page<WmsShipmentOrder> list = baseService.baseSelectPage(entity, ckid, hzid);
        return renderPage(list);
    }

    @PostMapping(value = "/listByBcAndWjh")
    @ApiOperation(value = "查询信息", response = JSONObject.class, notes = "显示创建完波次以及未拣货的订单")
    public @ResponseBody
    ResultPage<WmsShipmentOrder> listByBcAndWjh(@RequestBody WmsShipmentOrder entity, HttpSession session) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();

        }
        if (entity.getStime() != null && !entity.getStime().equals("")) {
            String[] line = entity.getStime().split("~");
            entity.setStime(line[0].trim());
            entity.setEtime(line[1].trim());
        }
        Page<WmsShipmentOrder> list = baseService.listByBcAndWjh(entity, ckid, hzid);
        return renderPage(list);
    }


    @PostMapping(value = "add")
    @ApiOperation(value = "新增信息", response = JSONObject.class, notes = "新增信息")
    public @ResponseBody
    JSONObject addWmsShipmentOrder(@RequestBody WmsShipmentOrder entity, HttpSession session) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        entity.setId(UUIDUtils.uuid());
        entity.setCjr(userinfo.getUser().getName());
        entity.setXgr(userinfo.getUser().getName());
        entity.setShr("");
        entity.setCkid(ckid);

        try {
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                entity.setSshzid(hzid);
                baseService.baseInsertSelective(entity);
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单增加成功", "", JSONObject.fromObject(entity).toString());

            } else {
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "货主为空，发运订单增加失败", "", "");

            }
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单增加成功", e.getMessage(), JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单增加失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单增加失败", e.getMessage(), "");
        }

    }

    /* @GetMapping(value = "getWmsShipmentOrder/{id}")
     @ApiOperation(value = "查看单个信息",response = JSONObject.class, notes = "GET查看单个信息")
     public @ResponseBody WmsShipmentOrder getWmsShipmentOrderById(@PathVariable("id") String id) throws ServiceException {
         WmsShipmentOrder entity=baseService.baseSelectByPrimaryKey(id);
         return entity;
     }*/
    @PostMapping(value = "edit")
    @ApiOperation(value = "编辑信息", response = JSONObject.class, notes = "编辑信息")
    public @ResponseBody
    JSONObject editWmsShipmentOrder(@RequestBody WmsShipmentOrder entity, HttpSession session) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        entity.setXgr(userinfo.getUser().getName());
        try {
            baseService.baseUpdateByPrimaryKeySelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单修改成功", "", JSONObject.fromObject(entity).toString());
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单修改成功", e.getMessage(), JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单修改失败", e.getMessage(), "");

        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单修改失败", e.getMessage(), "");

        }
    }

    @PostMapping(value = "getCusInfo")
    @ApiOperation(value = "获取客户信息", response = JSONObject.class, notes = " 查询输入</br>{\"khbh\":\"客户编号模糊查询\"}")
    public @ResponseBody
    JSONArray getCusInfo(HttpSession session, @RequestBody Map<String, String> map) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        String hzid = null;
        //货主
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        List<WmsCustomerFile> cusList = null;
        try {
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                cusList = baseService.getCusInfo(hzid, ckid, map.get("khbh"));
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(cusList);
    }

    @PostMapping(value = "getGoodsByHzid")
    @ApiOperation(value = "获取货品信息", response = JSONObject.class, notes = " 查询输入</br>{\"bh\":\"货品编号模糊查询\"}")
    public @ResponseBody
    JSONArray getGoodsByHzid(HttpSession session, @RequestBody Map<String, String> map) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        String hzid = null;
        //货主
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        List<WmsGoods> goodsList = null;
        try {
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                goodsList = baseService.getGoodsByHzid(hzid, ckid, map.get("bh"));
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(goodsList);
    }

    @PostMapping(value = "getLiLibraryPosition")
    @ApiOperation(value = "获取库位信息", response = JSONObject.class, notes = " 查询输入</br>{\"mc\":\"库位名称模糊查询\"}")
    public @ResponseBody
    JSONArray getLiLibraryPosition(HttpSession session, @RequestBody Map<String, String> map) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        String hzid = null;
        //货主
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        List<WmsLibraryPosition> list = null;
        try {
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                list = baseService.getLiLibraryPosition(hzid, ckid, map.get("mc"));
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(list);
    }


    @PostMapping(value = "getWmsStock")
    @ApiOperation(value = "获取库存信息（批次）", response = JSONObject.class, notes = " 查询输入</br>{\"hpid\":\"货品id\",\"hppc\":\"批次模糊查询\"}")
    public @ResponseBody
    JSONArray getWmsStock(HttpSession session, @RequestBody Map<String, String> map) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        String hzid = null;
        //货主
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        List<WmsStock> list = null;
        try {
            list = baseService.getWmsStock(map.get("hpid"), ckid, map.get("hppc"));
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(list);
    }

    @PostMapping(value = "getWmsStockScrq")
    @ApiOperation(value = "获取库存信息（生产日期）", response = JSONObject.class, notes = "查询输入</br>{\"hpid\":\"货品id\",\"scrq\":\"生产日期\"}")
    public @ResponseBody
    JSONArray getWmsStockScrq(HttpSession session, @RequestBody Map<String, String> map) {
        JSONArray json = new JSONArray();
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        String hzid = null;
        //货主
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        if (map.get("scrq") != null && map.get("scrq").equals("不指定")) {
            return json;
        } else {
            List<WmsStock> list = null;
            try {
                list = baseService.getWmsStockScrq(map.get("hpid"), ckid, map.get("scrq"));
            } catch (ServiceException e) {
                e.printStackTrace();
            }
            return JSONArray.fromObject(list);
        }

             /*f(!(map.get("scrq").equals("不指定"))){
                List<WmsStock> list = null;
                try {
                    list = baseService.getWmsStockScrq(map.get("hpid"), ckid, map.get("scrq"));
                } catch (ServiceException e) {
                    e.printStackTrace();
                }
                if(list.size()>0){
                    json = JSONArray.fromObject(list);
                }
            }*/
        //  return json;
    }

    @ApiOperation(value = "作废发运订单", response = JSONObject.class, notes = "发运订单作废，ids:选中的多条发运订单的id集合")
    @PostMapping(value = "updateSfyx")
    public @ResponseBody
    JSONObject updateSfyx(@RequestBody SetAndModifyModel setModel, HttpSession session) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        try {
            baseService.updateSfyx(setModel.getIds(), userinfo.getUser().getName());
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "作废发运订单成功", "", "");

        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "作废发运订单成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "作废发运订单失败", e.getMessage(), "");

        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "作废发运订单失败", e.getMessage(), "");
        }

    }


    @ApiOperation(value = "标记发运订单", response = JSONObject.class, notes = "发运订单标记，ids:选中的多条发运订单的id集合")
    @PostMapping(value = "updateZt")
    public @ResponseBody
    JSONObject updateZt(@RequestBody SetAndModifyModel setModel, String bjyy, HttpSession session) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        try {
            baseService.updateZt(setModel.getIds(), userinfo.getUser().getName(), bjyy);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单标记成功", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单标记成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单标记失败", e.getMessage(), "");

        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单标记失败", e.getMessage(), "");
        }

    }

    @ApiOperation(value = "审核发运订单", response = JSONObject.class, notes = "发运订单审核，ids:选中的多条发运订单的id集合")
    @PostMapping(value = "updateShzt")
    public @ResponseBody
    JSONObject updateShzt(@RequestBody SetAndModifyModel setModel, HttpSession session) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        try {
            baseService.updateShzt(setModel, userinfo.getUser().getName(), String.valueOf(userinfo.getUser().getOrgId().get(0)));
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单审核成功", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "发运订单审核成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单审核失败", e.getMessage(), "");

        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单审核失败", e.getMessage(), "");

        }

    }
 /*   @ApiOperation(value = "查看发运订单", response = JSONObject.class, notes = "查看发运订单")
    @PostMapping(value = "getWmsShipmentOrder")
    public @ResponseBody
    JSONObject getWmsShipmentOrder() {

        try {
            baseService.getWmsShipmentOrder();
            return SetStatus.setReturn(1, true, 1, "查看发运订单", "发运订单查看", "");

        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setReturn(1, true, 1, "查看发运订单", e.getMessage(), "");

        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setReturn(0, false, 1, "发运订单查看", e.getMessage(), "");

        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setReturn(0, false, 1, "查看发运订单", "发运订单查看", "");
        }

    }*/

    @ApiOperation(value = "编辑时查找对象", response = JSONObject.class, notes = "编辑时查找对象,id:发运订单id")
    @GetMapping(value = "getOrderAndDetail/{id}")
    public @ResponseBody
    JSONObject getOrderAndDetail(HttpSession session, @PathVariable String id) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        JSONObject jsonObject = null;
        try {
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                jsonObject = baseService.baseSelectByPrimaryKey(id, hzid, ckid);
            }
        } catch (ServiceException e) {
            e.printStackTrace();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    //小新星改动
    @ApiOperation(value = "编辑时查找明细集合", response = JSONObject.class, notes = "编辑时查找明细集合,fydid:发运订单id")
    @PostMapping(value = "getDetailList")
    public @ResponseBody
    JSONArray getDetailList(HttpSession session, @RequestBody SetAndModifyModel setAndModifyModel) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        JSONArray jsonArray = null;
        try {
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                jsonArray = baseService.getDetailList(setAndModifyModel.getId(), hzid, ckid);
            }

        } catch (ServiceException e) {
            e.printStackTrace();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    @ApiOperation(value = "导入发运订单", response = JSONObject.class, notes = "导入发运订单")
    @PostMapping(value = "importExcle")
    public @ResponseBody
    JSONObject importExcle(HttpSession session, @RequestBody Map<String, String> map) {
        try {
            baseService.importExcle(session, map.get("filepath"), map.get("type"));
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "导入发运订单及明细成功", "", "");

        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "导入发运订单及明细成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "导入发运订单及明细失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "导入发运订单及明细失败", e.getMessage(), "");
        }
    }

    @ApiOperation(value = "获取订单类型字典", response = JSONObject.class, notes = " 查询输入</br>{\"ddlx\":\"订单类型名称名称模糊查询\"}")
    @PostMapping(value = "getPoLx")
    public @ResponseBody
    JSONArray getPoLx(HttpSession session, @RequestBody Map<String, String> map) {
        List<WmsOwnerOrdertype> list = null;
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        try {
            //用户信息
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                list = baseService.getPoLx(hzid, ckid, map.get("ddlx"));
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(list);
    }
/*    @ApiOperation(value = "获取客户信息", response = JSONObject.class, notes = " 获取客户信息")
    @GetMapping(value = "getCusById/{id}")
    public @ResponseBody
    JSONObject getCusById(HttpSession session,@PathVariable String id) {
        WmsCustomerFile wmsCustomerFile=null;
        try {
            //用户信息
             wmsCustomerFile = baseService.getCusById(id);
            } catch (ServiceException e1) {
            e1.printStackTrace();
        }
        return JSONObject.fromObject(wmsCustomerFile);
    }*/


    @PostMapping(value = "getShipmentOrder")
    @ApiOperation(value = "查询信息", response = JSONObject.class, notes = "查询信息")
    public @ResponseBody
    ResultPage<WmsShipmentOrder> getShipmentOrder(@RequestBody WmsShipmentOrder entity, HttpSession session) {
        JSONObject jsonObject = new JSONObject();
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        //用户信息
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();
        }
        Page<WmsShipmentOrder> list = null;

        list = wmsOrderRepairService.getShipmentOrder(entity, ckid, hzid);

        return renderPage(list);
    }


    @ApiOperation(value = "获取字段对应的列名(发运主表)", response = JSONObject.class, notes = " 获取字段对应的列名(发运主表)")
    @GetMapping(value = "getFyColumnName")
    public @ResponseBody
    JSONArray getFyColumnName(HttpSession session) {
        List<WmsColumnName> list = new ArrayList<WmsColumnName>();
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        try {
            //用户信息
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                list = baseService.getFyColumnName(hzid, ckid);
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(list);
    }

    @ApiOperation(value = "获取字段对应的列名(发运明细)", response = JSONObject.class, notes = " 获取字段对应的列名(发运明细)")
    @GetMapping(value = "getFymxColumnName")
    public @ResponseBody
    JSONArray getFymxColumnName(HttpSession session) {
        List<WmsColumnName> list = new ArrayList<WmsColumnName>();
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        try {
            //用户信息
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                list = baseService.getFymxColumnName(hzid, ckid);
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(list);
    }


    @ApiOperation(value = "根据列名查找该列对应的字典", response = JSONObject.class, notes = "zcloumn:列名 ")
    @GetMapping(value = "getDictionary/{zcolumn}")
    public @ResponseBody
    JSONArray getDictionary(HttpSession session, @PathVariable String zcolumn) {
        List<WmsCustomDictionary> list = null;
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        try {
            //用户信息
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                list = baseService.getDictionary(zcolumn, hzid, ckid);
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONArray.fromObject(list);
    }


    @ApiOperation(value = "根据货品id 查询货品库存", response = JSONObject.class, notes = "查询输入</br>{\"hpid\":\"货品id\",\"page\":\"页\"，\"rows\":\"行\"}")
    @PostMapping(value = "getGoodsStock")
    public @ResponseBody
    JSONObject getGoodsStock(HttpSession session, @RequestBody Map map) {
        Page<WmsStock> list = null;
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        try {
            //用户信息
            list = baseService.getGoodsStock((String) map.get("hpid"), (Integer) map.get("page"), (Integer) map.get("rows"), ckid);
            return SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
    }


    @ApiOperation(value = "查询库存余量", response = JSONObject.class, notes = "查询输入</br>{\"ddid\":\"发运订单id\",\"page\":\"0\",\"rows\":\"10\"}")
    @PostMapping(value = "getStockYl")
    public @ResponseBody
    JSONObject getStockYl(HttpSession session, @RequestBody Map map) {
        Page<WmsShipmentOrderDetailed> list = null;
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        try {
            //用户信息
            list = baseService.getStockYl((String) map.get("ddid"), (Integer) map.get("page"), (Integer) map.get("rows"));
            return SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
    }


    @PostMapping(value = "copyOrder")
    @ApiOperation(value = "复制单据", response = JSONObject.class, notes = "复制单据")
    public @ResponseBody
    JSONObject copyOrder(@RequestBody SetAndModifyModel setModel, HttpSession session) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        try {
            //用户信息
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                baseService.copyOrder(setModel.getId(), hzid, ckid);
            }
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "复制单据成功", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "复制单据成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "复制单据失败", e.getMessage(), "");

        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "发运订单修改失败", e.getMessage(), "");

        }
    }


    @PostMapping(value = "updateJhwjzt")
    @ApiOperation(value = "借货完结", response = JSONObject.class, notes = "借货完结")
    public @ResponseBody
    JSONObject updateJhwjzt(@RequestBody SetAndModifyModel setModel, HttpSession session) {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        try {

            baseService.updateJhwjzt(setModel.getId());
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "借货完结成功", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "借货完结成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "借货完结失败", e.getMessage(), "");

        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "借货完结失败", e.getMessage(), "");

        }
    }

    /**
     * 标记暂不发货
     *
     * @param session
     * @param map
     * @return
     */
    @ApiOperation(value = "标记暂不发货", response = JSONObject.class, notes = "查询输入</br>{\"id\":\"发运订单明细id\",\"zbfh\":\"0\"}")
    @PostMapping(value = "updateZbfh")
    public @ResponseBody
    JSONObject updateZbfh(HttpSession session, @RequestBody Map map) {
        try {

            baseService.updateZbfh((String) map.get("id"), (String) map.get("zbfh"));
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "标记成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "标记失败", e.getMessage(), "");

        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "标记失败", e.getMessage(), "");

        }


    }

    @PostMapping(value = "zbfhListExport")
    @ApiOperation(value = "导出暂不发货订单信息", response = JSONObject.class, notes = "导出暂不发货订单信息")
    public @ResponseBody
    void baseSelectPageZBFH(@RequestBody WmsShipmentOrder entity, HttpSession session, HttpServletResponse response) throws IOException {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        entity.setCkid(ckid);
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();

        }
        entity.setSshzid(hzid);
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("采购订单表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<WmsShipmentOrder> list = baseService.baseSelectPageZBFH(entity);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsShipmentOrder.class, list);
        workbook.write(response.getOutputStream());
    }


    @PostMapping(value = "/selectCyslb")
    @ApiOperation(value = "查询承运商类别", response = JSONObject.class, notes = "查询承运商类别")
    public @ResponseBody
    List<SysDictionary> selectCyslb(@RequestBody Map map) {
        List<SysDictionary> list = baseService.selectCyslb((String) map.get("d_name"));
        return list;
    }


    /**
     * 标记暂不发货
     *
     * @param session
     * @param map
     * @return
     */
    @ApiOperation(value = "修改承运商类别", response = JSONObject.class, notes = "查询输入</br>{\"ids\":\"选中的多条发运订单id\",\"cyslb\":\"0\"}")
    @PostMapping(value = "updateCyslb")
    public @ResponseBody
    JSONObject updateCyslb(HttpSession session, @RequestBody Map map) {
        try {

            baseService.updateCyslb((List) map.get("ids"), (String) map.get("cyslb"));
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "", "", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "修改成功", e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "修改失败", e.getMessage(), "");

        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "修改失败", e.getMessage(), "");

        }


    }

    @ApiOperation(value = "查询批量打印快递面单的发运订单", response = JSONObject.class, notes = "查询输入</br>{\"page\":\"1\",\"rows\":\"10\"}")
    @PostMapping(value = "selectPrintOrder")

    public @ResponseBody
    JSONObject selectPrintOrder(@RequestBody Map map) {
        List<WmsShipmentOrder> list = null;
        Integer count = 0;
        try {
            list = baseService.selectPrintOrder((Integer) map.get("page"), (Integer) map.get("rows"), (String) map.get("print"), (String) map.get("fybh"), (String) map.get("bcbh"));
            count = baseService.selectPrintOrderCount((String) map.get("print"), (String) map.get("fybh"), (String) map.get("bcbh"));
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SetStatus.setSearchType(JSONArray.fromObject(list).toString(), count);

    }
    //承运商预修改
    @PostMapping(value = "/getupdatecys")
    @ApiOperation(value = "预修改查询承运商", response = JSONObject.class, notes = "查询承运商")
    public @ResponseBody
    JSONObject getupdatecys(@RequestBody SetAndModifyModel setModel) {
        JSONObject json = new JSONObject();
        WmsShipmentOrder entity = new WmsShipmentOrder();
        try {

            entity = baseService.getOneCys(setModel.getId());
            JSONObject jsonObject3 = new JSONObject();
            if(entity.getShow_cyslb()!=null&& entity.getShow_cyslb()!="") {
                jsonObject3.accumulate("id", entity.getCyslb());
                jsonObject3.accumulate("text", entity.getShow_cyslb());
            }else{
                jsonObject3.accumulate("id", "");
                jsonObject3.accumulate("text", "");
            }
            String str= com.alibaba.fastjson.JSONObject.toJSONString(entity);
            json=JSONObject.fromObject(str);
            json.accumulate("telephone",jsonObject3);

        } catch (Exception e) {
            e.printStackTrace();
            //json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"编辑库区信息失败","编辑库区信息失败","");
        }
        return json;
    }


}
